/*
 * @Author: yangrongxin
 * @Date: 2021-08-16 15:20:21
 * @LastEditors: yangrongxin
 * @LastEditTime: 2021-12-22 15:43:33
 */

import Taro from '@tarojs/taro';
import { STORAGE_TYPE } from '@/const/dataDictionary';
import type {
  QuestionsInfo,
} from '../types/exam.d';
import {
  QUESTION_TYPE
} from '../types/exam.d';
import { getItemSync } from './storage';

/* 根据身份证号 获取当前登录人出生年月日 */
export const getBirthDay = (idCard: string) => {
  let birthday = '';
  if (!idCard || (idCard.length !== 15 && idCard.length !== 18)) return birthday;
  if (idCard.length === 15) {
    birthday = `19${idCard.substring(6, 8)}-${idCard.substring(8, 10)}-${idCard.substring(10, 12)}`;
  } else if (idCard.length === 18) {
    birthday = `${idCard.substring(6, 10)}-${idCard.substring(10, 12)}-${idCard.substring(12, 14)}`;
  }
  return birthday;
};

// 判断当前用户是否已经登录 如果登录的话跳转对应页面 没有登录的话 回到登录页面
export const loginWithFn = (callback?: any) => {
  const isLogin = getItemSync(STORAGE_TYPE.ISLOGIN); // 获取当前用户是不是已经登录的状态
  if (Number(isLogin) === 1) {
    // 执行对应的回调事件 跳转到登录的界面上
    callback && callback();
  } else {
    Taro.navigateTo({
      url: '/pages/Login/index',
    });
  }
};

// 判断当前用户是不是已经登录的状态
export const isLoginFn = () => {
  const isLoginStatus = getItemSync(STORAGE_TYPE.ISLOGIN); // 获取当前用户是不是已经登录的状态
  return Number(isLoginStatus) === 1;
};

// 每个问题需要的标准时间范围
export const caclTime = (question: QuestionsInfo) => {
  let len: number = 0;
  const { title, extend, type } = question;
  // 根据当前的题目的长度 获取当前的答题最大和最小的时间
  // 根据当前的题目类型 不同的题目应该有不同的计算规则
  switch (type) {
    case QUESTION_TYPE.SINGLE:
    case QUESTION_TYPE.MUILT:
      len =
        title.length +
        extend?.reduce((lenth: number, next: any) => {
          return lenth + next.content.length;
        }, 0);
    ;break;
    case QUESTION_TYPE.NPS:
      len = title.length + extend?.beginScore;
    ;break;
    default:;break; // 默认不进行计算
  }

  const minNum = 0.05 * len + 1;
  const maxNum = 0.25 * len + 3;
  return {
    minNum,
    maxNum,
  };
};
